
var windowheight = $(window).height();
var tabtop = $('.j-tab').offset().top;

$(window).scroll(function(){
  var scrollTop = $(this).scrollTop(); //页面滚动的高度
  if(scrollTop >= tabtop){
    $('.j-tab').addClass('fixed');
  }else{
    $('.j-tab').removeClass('fixed');
  }
});


var appUserId = null, 
    timeing = null,
  appClientLable = null;
function getparameter(str){
  appUserId = str.split(',')[0];
  appClientLable = str.split(',')[1];
  votelist({'appUserId': appUserId,'appClientLable': appClientLable});
  openVoteDetail = openNewWindow;
}
// wxShare('测试', '分享来自经济日报客户端，更多精彩请下载http://d.jingjiribao.cn/index.html', 'https://share.jingjiribao.cn/static/imgs/icon-300.png'); 
//详情显隐
$('#j-activeinfo').click(function(){
  if( $('#j-activeintro').hasClass('fn-hide')){
    $(this).text('收起');
    $('#j-activeintro').removeClass('fn-hide')
  }else{
    $(this).text('详情');
    $('#j-activeintro').addClass('fn-hide');
  }

  // $('.j-activeintro').toggle('1000');
});

//tab显隐
$('.j-tabcell').click(function () {
  var target = $(this).data('target');
  console.log(target)
  if(target != 'j-detail'){
     if(target == 'j-leader'){
      getRankList(function(resData){
        $('.j-leader').html(juicer($('#j-leadertpl').html(), {"data":resData}));
        var icon = ['first-icon', 'two-icon', 'three-icon'];
        var _count = 0,
          _order = 0;
        $('.j-leader .lead-box').each(function(){
          if ($(this).data('count') == _count) {
            $(this).data('order', _order);
          } else {
            _count = $(this).data('count');
            _order = $(this).data('order');
          }
        });
        $('.j-leader .lead-box').each(function(){
          var thisOrder = $(this).data('order');
          if (thisOrder < 3) {
            $(this).find('.left-info').addClass(icon[thisOrder]);
            $(this).find('.righttext').addClass('hoticon');
          } else {
            $(this).find('.left-info').html(thisOrder + 1);
          }
        });
      });

    }
    $('body').css('background','#fff');
  }else{
   
    $('body').css('background','#000');
  }
  $(this).addClass('tabcell-active').siblings().removeClass('tabcell-active');
  $('.tab').removeClass('tab-active');
  $('.'+target).addClass('tab-active')
});


// 倒计时
function gotime(startDate, endDate){
  var startDate = + new Date(Date.parse(startDate.replace(/-/g,  "/"))),
    endDate = + new Date(Date.parse(endDate.replace(/-/g,  "/"))),
    xtimes = 0;
  function timeFn(){
    var _time = 0,serverDate = null;
      serverDate = + new Date() + xtimes;
    if (serverDate < startDate) {
      _time = startDate - serverDate;
    } else if (serverDate < endDate) {
      _time = endDate - serverDate;
    } else {
      if (timeing) clearInterval(timeing);
    }
    if (_time == 0) {
      $('#j-header .timebar').html('');
      return false;
    }
    var _day = Math.floor(_time / (24 * 60 * 60 * 1000));
    _time = _time % (24 * 60 * 60 * 1000);
    var _hh = Math.floor(_time / (60 * 60 * 1000));
    _time = _time % (60 * 60 * 1000);
    var _mm = Math.floor(_time / (60 * 1000));
    _time = _time % (60 * 1000);
    var _ss = Math.floor(_time / 1000);
    var _html = '';
    _html += '<span>' + _day + '</span><span>天</span>';
    _html += '<span>' + _hh + '</span><span>时</span>';
    _html += '<span>' + _mm + '</span><span>分</span>';
    // if (_day > 0) _html += '<span>' + _day + '</span><span>天</span>';
    // if (_day > 0 || _hh > 0) _html += '<span>' + _hh + '</span><span>时</span>';
    // if (_day > 0 || _hh > 0 || _mm > 0) _html += '<span>' + _mm + '</span><span>分</span>';
    _html += '<span>' + _ss + '</span><span>秒</span>';
    $('#j-header .timebar').html(_html);
  }
  $.ajax({cache: false, success: function(data, status, xhr){
    xtimes = + new Date(xhr.getResponseHeader('Date')) - (+ new Date());
    timeing = setInterval(timeFn, 500);
  }});
}

function votelist(param){
  getVoteList(param,function(resData){
    clearInterval(timeing);
    document.title = resData['title'];
     wxShare(resData['title'], '分享来自经济日报客户端，更多精彩请下载http://d.jingjiribao.cn/index.html', resData['shareurl']); 
    if($('#j-header').find('img').length){
      $('#j-header').find('img').attr('src',resData['backgroundImg']);
    }else{
      $('#j-header').append('<img src="'+resData['backgroundImg']+'">');
    }

    
    if(resData['remark']){
      $('#j-activeinfo').removeClass('fn-hide');
      $('#j-activeintro').html(resData['remark']);
    }
    
    $('#j-joinnum').html(resData['userCount']);
    //根据type模板渲染0=二图，1=纯文字，2=左图右文，3=通栏
    if(resData['type'] == '0'){
      $('#j-votelist1 .leftfall').html('');
      $('#j-votelist1 .rightfall').html('');
      var voteObj1 = juicer($('#j-votetpl4').html(), resData);
      $(voteObj1).find('.card-column').forEach(function(item,idx){

        var leftheight  = $('#j-votelist1 .leftfall').height();
        var rightheight = $('#j-votelist1 .rightfall').height();
        if(leftheight == rightheight || leftheight < rightheight){
          $('#j-votelist1 .leftfall').append(item);
        }else{
          $('#j-votelist1 .rightfall').append(item);
        }

      });

      //原两图标题省略样式
      // $('#j-votelist').html(juicer($('#j-votetpl1').html(), resData));

    }else if(resData['type'] == '2'){
      $('#j-votelist').html(juicer($('#j-votetpl2').html(), resData));
    }
    else if(resData['type'] == '3'){
      $('#j-votelist').html(juicer($('#j-votetpl3').html(), resData));
    }else if(resData['type'] == '1'){
      $('#j-votelist').html(juicer($('#j-votetpl5').html(), resData));
    }

    $('.j-rule .rule-box').html(resData['principle']);
    if(resData['votestatus'] == 0){
      var myDate =   new Date(Date.parse(resData['startdate'].replace(/-/g,  "/")));
      var year = myDate.getFullYear();
      var month = myDate.getMonth() + 1;
      var day  = myDate.getDate();
      $('.timebar').text(year+ '年'+ month+'月'+day+'日开启').addClass('text');
    }else if(resData['votestatus'] == 1){
      gotime(resData['startdate'], resData['enddate']);
    }else if(resData['votestatus'] == 2){
      $('.timebar').text('活动已结束').addClass('text');
    }


    // 投票
    $('.j-votebtn').on(tap, function() {
      var id = $(this).data('id');
      var status = $(this).data('status');
      var needlogin = $(this).data('needlog');

      var _this = this;
      var votenum = parseInt($(this).parents('.j-card').find('.j-votenum').text());
      if(status == 0){
        toast.success('活动暂未开始',1500);
      }else if(status == 1){
        submitVote(id, needlogin, function(res){
          toast.success(res.message,1500);
          if(res.data == false){//投票次数达到上限
            $(_this).text('感谢支持').removeClass('j-votebtn').addClass('outbtn');

            $(_this).parents('.j-card').data('checked','1');
          }
          
          votenum++;
          $(_this).parents('.j-card').find('.j-votenum').text(votenum);
        });
      }else if(status == 2){
        toast.success('活动已结束',1500);
      }
      return false;
    });

    //小花  点击图片跳转详情页
    ['#j-votelist','#j-votelist1'].forEach(function(item){
    // ['#j-votelist'].forEach(function(item){
      
      $(item).find('.j-jumptodetail').click(function (){
        var optionId = $(this).parents('.j-card').data('id');
        var status = $(this).parents('.j-card').data('status');
        var checked = $(this).parents('.j-card').data('checked');
        var needlog = $(this).parents('.j-card').data('needlog');
        var hasDetail = $(this).parents('.j-card').data('hasdetail');
        
        if(hasDetail){
          window.location.href="/active/vote/detail.html?id="+voteParams['id']+"&optionid="+ optionId+"&status="+status+"&checked="+checked+"&needlog="+ needlog+"&newsId="+voteParams['newsId'];
        }
        

      });
    });
    
  });
}

var userAgent = window.navigator.userAgent.toLocaleLowerCase();
if (userAgent.indexOf('jjrb') > -1) {//在端内
}else{
  votelist({'appUserId': null,'appClientLable': null});
  $('.j-banner').css('display','block');
  $('.j-detail,.j-leader,.j-rule').css('margin-bottom','1.333rem')
}


getRankList(function(resData){
  $('.j-leader').html(juicer($('#j-leadertpl').html(), {"data":resData}));
  var icon = ['first-icon', 'two-icon', 'three-icon'];
  var _count = 0,
    _order = 0;
  $('.j-leader .lead-box').each(function(){
    if ($(this).data('count') == _count) {
      $(this).data('order', _order);
    } else {
      _count = $(this).data('count');
      _order = $(this).data('order');
    }
  });
  $('.j-leader .lead-box').each(function(){
    var thisOrder = $(this).data('order');
    if (thisOrder < 3) {
      $(this).find('.left-info').addClass(icon[thisOrder]);
      $(this).find('.righttext').addClass('hoticon');
    } else {
      $(this).find('.left-info').html(thisOrder + 1);
    }
  });
});






